package com.acme.bankapp.service;

import java.io.IOException;

import com.acme.bankapp.dal.BankDataService;
import com.acme.bankapp.dal.InvalidFeedFormatException;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.client.ClientExistsException;
import com.acme.bankapp.domain.client.Gender;

public class BankApplication {

	private static final String ERR_UNSUPPORTED_ARGS = "Unsupported arguments";

	public static void main(String[] args) throws ClientExistsException,
			IOException, InvalidFeedFormatException, ClassNotFoundException {
		
		Bank bank;
		
		if (args.length == 0) {
			bank = createSampleBank();
			
		} else if (args.length != 2) {
			System.out.println(ERR_UNSUPPORTED_ARGS);
			return;
			
		} else {
			switch (args[0]) {
				case "-loadfeed":
					bank = createBankFromFeed(args[1]);
					break;
		
				case "-loadbank":
					bank = createBankFromFile(args[1]);
					break;
		
				default:
					System.out.println(ERR_UNSUPPORTED_ARGS);
					return;
			}
		}
		
		System.out.println(bank.getClients());
	}

	private static Bank createSampleBank() throws ClientExistsException, IOException {
		Bank bank = new Bank();
		
		BankService.addClient(bank, new Client("Lizzie Bennet", Gender.FEMALE,
				new SavingAccount(1000)));
		BankService.addClient(bank, new Client("Oleg Portnov", Gender.MALE,
				new SavingAccount(300)));
		BankService.addClient(bank, new Client("Tereza Lisbon", Gender.FEMALE,
				new CheckingAccount(400, 100)));
		
		return bank;

	}

	private static Bank createBankFromFeed(String path) throws ClientExistsException,
			IOException, InvalidFeedFormatException {
		
		Bank bank = new Bank();
		BankDataService.loadFeedFromFile(bank, path);
		return bank;
	}

	private static Bank createBankFromFile(String path) throws ClassNotFoundException,
			IOException {
		
		return BankDataService.readBankFromFile(path);
	}
}